users = {
    edit_users : function(elements){
	$("body").on("click",elements,function(){
	    var form_elements = '#update_user';
	    var form_name = $(form_elements);
	    $(form_name).trigger("reset");
	    var data_id = $(this).parent().attr("data-id");
	    var json_data = $("body").data("data_" + data_id);
		 console.log(data_id);
		 if (json_data != undefined) {
				var json_data = $("body").data("data_" + data_id);	
				console.log(json_data)
		 }else{
				var json_data_val = $("#" + data_id).val();
				var json_data = JSON.parse(json_data_val);
				console.log(json_data)
		 }
	   
	    
	    // Input Information on the modal editting
	    $('input[name="first_name"]').val(json_data['first_name']);
	    $('input[name="last_name"]').val(json_data['last_name']);
	    $('input[name="email"]').val(json_data['uEmail']);
	    $('input[name="username"]').val(json_data['username']);
	
	    $('select[name="access_role"] option[value=' + json_data['rRoleID'] + ']').attr('selected',true);
	    $('select[name="department_unit"] option[value=' + json_data['department'] + ']').attr('selected',true);
	    $('select[name="brand_category"] option[value=' + json_data['bBrandID'] + ']').attr('selected',true);
		 $encrypt_id = MD5(MD5(json_data['uID']));
		 $('input[name="file"]').attr("data-loc","img-file-" + $encrypt_id);
		 
		 var img = $(".img-file-" + $encrypt_id).attr("src");
		
		 var img_loc = '<img data-placement="bottom" data-original-title="' + json_data['first_name'] + ' ' + json_data['last_name'] + '" src="' + img + '" width="50" height="50" class="img-file-' + $encrypt_id + ' img-polaroid userAvatar">';
		 $("#img_loc").html(img_loc);
		 
	    if (json_data['uIs_active'] == "1") {
		$checked = "checked";
		$('input[name="set_active"]').attr($checked,true);
	    }else{
		$checked = "";
	    }
	    $('input[name="set_active"]').parent().addClass($checked);
	    
	    
	    // Append ID
	    $("input[name='user_id']").val(json_data.uID);
	    // Updating Of user    
	    var options = { 
		//target:        '#save',
		dataType:      'json',
		beforeSubmit:  showRequest,
		success:       showResponse
	    };
	    //console.log($('#update_user').validationEngine())
	    form_name.validationEngine('attach');
	    form_name.ajaxForm(options);
	    
	})
    },
    
    trigger_search_users : function(){
    
	$("body").on("click","#trigger_search_users",function(){
	    var search_val = $('input[name="search_users"]').val();
	    
	    var num_set = $("#numItems").val();
	    
	    var json_parse = {
			    set_data 	: {action : "search_user",search_val:search_val},
			    set_paginate_json : {action : "search_user",search_val:search_val},
			    set_paginate_data : "false",
			    set_url	: "/ajax/load_users",
			    set_html_element : "/ajax/load_users",
			    set_from	: 0,
			    set_to	: 10,
			    num		: num_set,
			    elements	: "#pagination"
	    }
	    data_all(json_parse);
	
	    
	})
    
    }
}
paceOptions = {
  elements: {
    selectors: ['.bg_light_gray']
  }
};
$(document).ready(function(){

    // Load All User List
    limitText(".user_name_list", 14);

     //var items = $("#content tbody tr");
    var numItems = $("#numItems").val();
    var perPage = 10;

    // now setup pagination
    $("#pagination").pagination({
	items: numItems,
	itemsOnPage: perPage,
	cssStyle: "",
	onPageClick: function(pageNumber) { // this is where the magic happens
	    // someone changed page, lets hide/show trs appropriately
	    var showFrom = perPage * (pageNumber - 1);
	    var showTo = showFrom + perPage;
	    
	    var div_wrapper_block = $('#user_list_wrapper');
	    // Block Div
	    div_wrapper_block.block({ 
		message: '<h4><img src="/images/loaders/3d_4.gif" /> Processing</h4>'
	    });
			
	    $.ajax({
		type : "POST",
		url  : "/ajax/load_users",
		data : {action : "load_all_users",select_to:showTo,select_from:showFrom},
		success : function(data){
		    //console.log(data)
		    var json_data = JSON.parse(data);
		    console.log(json_data)
		    var user_list = json_data['data'];
		    
		    var ret = "";
		    for(var i = 0; i < user_list.length; i++){
			$("body").data("data_" + user_list[i]['others']['encrypt_uID'],user_list[i]['user']); // Save to body.data
			ret += '<div class="span3" style="margin-left:0px !important;padding:0px 10px 0px 10px !important;">';            
			    ret += '<div class="userCard">';
				ret += '<div class="image">';
				    ret += user_list[i]['others']['img'];
				ret += '</div>';
				ret += '<div class="info" data-id="' + user_list[i]['others']['encrypt_uID'] + '">';
				    ret += '<h3 class="user_name_list">' + capitalize(user_list[i]['user']['first_name']) + " " + capitalize(user_list[i]['user']['last_name']) + '</h3>';
				    ret += '<p><strong>' + user_list[i]['user']['rRoleName'] + '</strong></p>';
				    ret += '<button class="btn edit_user"><a href="#fModal" role="button" data-toggle="modal">Edit</a></button> ';
				    ret += '<button class="btn remove_user">Remove</button>';
				    //ret += '<button class="btn edit_users">Edit</button>';
				    ret += '<div class="informer" style="width: 100%;text-align: right;">';
					ret += '<span>';
					    if(user_list[i]['user']['bBrandCategory'] != null){
						ret += user_list[i]['user']['bBrandCategory'];
					    }else{
						ret += "None";
					    }
					ret += '</span>';
				    ret += '</div>';
				ret += '</div>';
			    ret += '</div>';
			ret += '</div>';
			$("body").data("data_" + user_list[i])
		    }
		    $("#user_list").html(ret);
		    $("#numItems").val(json_data['count_users']);
			 limitText(".user_name_list", 14);
		    div_wrapper_block.unblock(); 
		}
	    })
	}
    });
    // Adding OF users    
    var options = { 
	//target:        '#save',
	dataType:      'json',
	beforeSubmit:  showRequest,
	success:       showResponse
    }; 
    $('#add_user').validationEngine('attach');
    $('#add_user').ajaxForm(options);

    // Editting Users	
    users.edit_users(".edit_user");
    
    // Search User
    users.trigger_search_users();
	 
	 // Editing user Profile Auth
	 var options = { 
	//target:        '#save',
	dataType:      'json',
	beforeSubmit:  showRequest,
	success:       showResponse
    }; 
    $('#edit_profile_user').validationEngine('attach');
    $('#edit_profile_user').ajaxForm(options);

    // Editting Users	
    users.edit_users(".edit_profile_user");

})




